package com.kasax.hipermission;

import android.app.Activity;
import android.os.Build;
import android.os.Environment;

import java.lang.ref.WeakReference;

public class HiPermissionManager {

    private PermissionProcessor permissionProcessor;

    private HiPermissionManager() {

    }

    public static HiPermissionManager getInstance() {
        return Holder.INSTANCE;
    }

    public boolean hasSdkCardPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            return Environment.isExternalStorageManager();
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return permissionProcessor.checkPermission(PermissionConstants.SDCARD_PERMISSION_GROUP);
        } else {
            return true;
        }
    }

    public void requestSdCardPermission() {
        permissionProcessor.processSdCardGroupPermission();
    }

    public void onActivityPermissionResult(int requestCode) {
        permissionProcessor.onActivityPermissionResult(requestCode);
    }

    public void onRequestPermissionsResult(int requestCode, String[] permissions) {
        permissionProcessor.onRequestPermissionsResult(requestCode, permissions);
    }

    public void attach(Activity activity) {
        permissionProcessor = new PermissionProcessor(new WeakReference<Activity>(activity));
    }

    public void injectCallback(PermissionProcessor.OnPmrmissionResultCallback callback) {
        permissionProcessor.injectCallback(callback);
    }

    public void detach() {
        permissionProcessor.detach();
    }

    private static class Holder {
        public static final HiPermissionManager INSTANCE = new HiPermissionManager();
    }

}
